package com.weme.mine.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import androidx.core.content.res.ResourcesCompat
import androidx.viewpager2.widget.ViewPager2
import com.weme.common.util.dp2px
import com.weme.common.view.ScaleTransitionPagerTitleView
import com.weme.mine.R
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator


fun MagicIndicator.bind(viewPager: ViewPager2?) {
    viewPager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            this@bind.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }

        override fun onPageScrollStateChanged(state: Int) {
            this@bind.onPageScrollStateChanged(state)
        }

        override fun onPageSelected(position: Int) {
            this@bind.onPageSelected(position)
        }
    })
}

class FollwingIndicator @JvmOverloads constructor(
    context: Context,
    set: AttributeSet? = null,
) : MagicIndicator(context, set) {

    private var mViewPager: ViewPager2? = null

    private var labels: List<String>? = null

    fun attachViewPager(viewPager: ViewPager2, label: List<String>) {
        mViewPager = viewPager
        labels = label

        init(viewPager.context)
        bind(mViewPager)
    }

    private fun init(context: Context) {
        val navigator = CommonNavigator(context)
        navigator.isAdjustMode = true
        navigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = labels?.size ?: 0

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val titleView = ScaleTransitionPagerTitleView(context)
                titleView.normalColor = Color.parseColor("#FFFFFFFF")
                titleView.text = labels?.get(index) ?: ""
                titleView.selectedColor = Color.WHITE
                titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                val typefaceBook = ResourcesCompat.getFont(context, com.example.resources.R.font.book)
                val typefaceBold = ResourcesCompat.getFont(context, com.example.resources.R.font.bold)
                titleView.typeface = typefaceBook
                titleView.setOnClickListener {
                    mViewPager?.setCurrentItem(index,false)
                }
                titleView.setTitleSelectListener(object : ScaleTransitionPagerTitleView.OnTitleSelectListener{
                    override fun onTitleSelect(index: Int) {
                        titleView.typeface = typefaceBold
                    }

                    override fun onTitleDeselect(index: Int) {
                        titleView.typeface = typefaceBook
                    }
                })
                return titleView
            }

            override fun getIndicator(p0: Context): IPagerIndicator {
                val linePagerIndicator = LinePagerIndicator(context)

                linePagerIndicator.roundRadius = context.dp2px(8).toFloat()
                linePagerIndicator.setColors(Color.parseColor("#4D9146FF"))
                linePagerIndicator.mode = LinePagerIndicator.MODE_MATCH_EDGE
                linePagerIndicator.lineHeight = context.dp2px(32).toFloat()
                return linePagerIndicator
            }
        }
        setNavigator(navigator)
    }
}